/**
 * Copyright (C) 2007 Transmute Technologies. All rights reserved.
 * ===============================================================
 * Component Desc : 
 * Major :
 * Minor :
 * @author Naveen Kumar
 * Mail : unaveenkumar@gmail.com
 * Created on : Jan 14, 2007
 *
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 *
 * 
 * Modified by : Jan 14, 2007
 * Revision number : 0.5
 * 
 * Revision History
 * ----------------
 * 
 */
package org.transmute.dao;

/**
 * <p/> ConnectionDetails holds the database connection oriented data.
 * </p>
 * @author Naveen Kumar
 */
public class ConnectionDetails {

	private int cacheTimeOut;

	private String dbUrl;

	private String driverUrl;

	private String password;

	private int timeOut;

	private String userId;

	/**
	 * <p/> No argument Constructor to intialize the newly created
	 * ConnectionDetails object with default values
	 * </p>
	 */
	private ConnectionDetails() {
		this(null, null, null, null);
	}

	/**
	 * <p/> Argument Constructor to intialize the newly created
	 * ConnectionDetails object instance attribute values
	 * </p>
	 * 
	 * @param String -
	 *            Driver URL.
	 * @param String -
	 *            Database URL.
	 * @param String -
	 *            User id.
	 * @param String -
	 *            Password.
	 */
	public ConnectionDetails(String driverUrl, String dbUrl, String userId,
			String password) {
		this.driverUrl = driverUrl;
		this.dbUrl = dbUrl;
		this.userId = userId;
		this.password = password;
		cacheTimeOut = 3 * 60 * 1000;
		timeOut = 3 * 60 * 1000;
	}

	/**
	 * <p/> Argument Constructor to intialize the newly created
	 * ConnectionDetails object instance attribute values
	 * </p>
	 * 
	 * @param String -
	 *            Driver URL.
	 * @param String -
	 *            Database URL.
	 * @param String -
	 *            User id.
	 * @param String -
	 *            Password.
	 * @param int -
	 *            Cache TimeOut in milli sec
	 * 
	 * @param int -
	 *            Time Out in milli sec
	 * 
	 */
	public ConnectionDetails(String driverUrl, String dbUrl, String userId,
			String password, int cacheTimeOut, int timeOut) {
		this.driverUrl = driverUrl;
		this.dbUrl = dbUrl;
		this.userId = userId;
		this.password = password;
		this.cacheTimeOut = cacheTimeOut;
		this.timeOut = timeOut;
	}

	/**
	 * <p/> equals method verifies wether both the objects are same, checks only
	 * the mandatory fields. So its advisable not to interchange the object
	 * eventhough they are same(it is used for internal implementation).
	 * </p>
	 * 
	 * @param ConnectionDetails -
	 *            Connection details.
	 * @return boolean - Returns true if mandatory fields are equal.
	 * @see ConnectionDetails
	 */
	public boolean equals(ConnectionDetails connDetails) {
		boolean flag = false;
		try {
			if (connDetails != null) {
				if (connDetails.getUserId().equals(getUserId())
						&& connDetails.getDbUrl().equals(getDbUrl())) {
					flag = true;
				}
			}
		} catch (Exception exp) {
		}
		return flag;
	}

	/**
	 * <p/> equals method verifies wether both the objects are same, checks only
	 * the mandatory fields. So its advisable not to interchange the object
	 * eventhough they are same(it is used for internal implementation).
	 * </p>
	 * 
	 * @param ConnectionDetails -
	 *            Connection details.
	 * @return boolean - Returns true if mandatory fields are equal.
	 * @see ConnectionDetails
	 */
	public boolean equals(Object checkDetails) {
		Class currentInstance = this.getClass();
		if (!currentInstance.isInstance(checkDetails)) {
			return false;
		}
		return equals((ConnectionDetails) checkDetails);
	}

	/**
	 * <p/> Generates the key to be used in connectionPooling
	 * </p>
	 * 
	 * @return
	 */
	protected String formKey() {
		if ((userId == null) || (userId.length() == 0)) { // assuming
			return dbUrl;
		} else {
			return dbUrl + ':' + userId;
		}
	}

	/**
	 * <p/> gets cache timeout
	 * </p>
	 * 
	 * @return int - cache time out.
	 */
	public int getCacheTimeOut() {
		return cacheTimeOut;
	}

	/**
	 * <p/> gets database connect url
	 * </p>
	 * 
	 * @return String - Database connect url.
	 */
	public String getDbUrl() {
		return dbUrl;
	}

	/**
	 * <p/> gets database driver url
	 * </p>
	 * 
	 * @return String - Driver connect url.
	 */
	public String getDriverUrl() {
		return driverUrl;
	}

	/**
	 * <p/> gets database login password
	 * </p>
	 * 
	 * @return String - Database login password.
	 * @deprecated
	 */
	protected String getPassword() {
		return password;
	}

	/**
	 * <p/> gets connection login time out
	 * </p>
	 * 
	 * @return int - Connection login time out.
	 */
	public int getTimeOut() {
		return timeOut;
	}

	/**
	 * <p/> gets database login userid
	 * </p>
	 * 
	 * @return String - Database login userid.
	 */
	public String getUserId() {
		return userId;
	}

	/**
	 * <p/> Method to check weather all madatory fields are set in this object
	 * are not.
	 * </p>
	 * 
	 * @return boolean - true if all mandatory fields present otherwise false.
	 */
	public boolean isValid() {
		if (valid(getUserId()) && valid(getDbUrl()) && valid(getDriverUrl())) {
			return true;
		}
		return false;
	}

	/**
	 * <p/> sets cache time out to keep track amount of time connection need to
	 * be preserved.
	 * </p>
	 * 
	 * @param int -
	 *            connection idle time. Default idle time out is 3mins. (This
	 *            won't be applicable for minimum number of connection
	 *            specified.)
	 * @return void
	 */
	public void setCacheTimeOut(int cacheTimeOut) {
		this.cacheTimeOut = cacheTimeOut;
	}

	/**
	 * <p/> sets database connect url
	 * </p>
	 * 
	 * @param String -
	 *            Database connect url.
	 * @return void
	 */
	public void setDbUrl(String dbUrl) {
		if (valid(dbUrl)) {
			this.dbUrl = dbUrl;
		}
	}

	/**
	 * <p/> sets driver url to be used to connect with database.
	 * </p>
	 * 
	 * @param String -
	 *            Driver URL.
	 * @return void
	 */
	public void setDriverUrl(String driverUrl) {
		if (valid(driverUrl)) {
			this.driverUrl = driverUrl;
		}
	}

	/**
	 * <p/> sets database login password
	 * </p>
	 * 
	 * @param String -
	 *            Database login password.
	 * @return void
	 */
	public void setPassword(String dbPwd) {
		if (valid(dbPwd)) {
			password = dbPwd;
		}
	}

	/**
	 * <p/> Self generated hashcode.
	 * </p>
	 */
	/*
	 public int hashCode() {
	 String hashString = getUserId() + getDbUrl();
	 return hashString.hashCode();
	 }
	 */

	/**
	 * <p/> sets database connection login timeout in milli sec
	 * </p>
	 * 
	 * @param int -
	 *            connection time out. Default value is 3secs.
	 * @return void
	 */
	public void setTimeOut(int timeOut) {
		this.timeOut = timeOut;
	}

	/**
	 * <p/> sets database login userid
	 * </p>
	 * 
	 * @param String -
	 *            Database login id.
	 * @return void
	 */
	public void setUserId(String dbUserId) {
		if (valid(dbUserId)) {
			userId = dbUserId;
		}
	}

	/**
	 * <p/> Method to streamline all members of the instance to string format
	 * </p>
	 * 
	 * @return String - containing member and its value.
	 */
	public String toString() {
		StringBuffer buff = new StringBuffer(48);
		String delim = "|";

		buff.append("driverUrl=> ").append(driverUrl);
		buff.append(delim);

		buff.append("userid=> ").append(userId);
		buff.append(delim);

		buff.append("dbUrl=> ").append(dbUrl);
		buff.append(delim);

		buff.append("timeOut=>").append(timeOut);
		buff.append(delim);

		buff.append("cacheTimeOut=>").append(cacheTimeOut);

		return buff.toString();
	}

	/**
	 * <p/> checks the string validity for null and empty
	 * </p>
	 * 
	 * @return boolean - returns true if the string is not null and not empty.
	 */
	private boolean valid(String temp) {
		if ((temp != null) && (temp.trim().length() > 0)) {
			return true;
		}
		return false;
	}
}
